﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace EnblibTest
{
    public partial class UserEdit : Form
    {
        Test_User curUser = null;

        public UserEdit()
        {
            InitializeComponent();
        }

        public UserEdit(Test_User usr)
        {
            InitializeComponent();
            this.textBox1.Text = usr.No;
            this.textBox2.Text = usr.Name;
            this.textBox3.Text = usr.Dept;
            this.textBox4.Text = usr.Sex;
            curUser = usr;
        }

        

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (curUser != null)
            {
                Database db = GetData.CreateDataBase();
                DataAccessor<Test_User> userAccessor;
                IRowMapper<Test_User> mapper = MapBuilder<Test_User>.BuildAllProperties();
                userAccessor = db.CreateSqlStringAccessor("update Enblib set Name='" + textBox2.Text +
                    "',Dept='" + textBox3.Text +
                    "',Sex='" + textBox4.Text +
                    "' where No='" + textBox1.Text + "'",mapper);

                var list=userAccessor.Execute().ToList();
                if (list.Count == 0)
                {
                    MessageBox.Show("修改成功");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else 
                {
                    MessageBox.Show("修改失败");

                }
                //else { MessageBox.Show("修改失败"); }
                //userAccessor = db.CreateSqlStringAccessor(""
                //Database db = GetData.CreateDataBase();
                //StringBuilder sb = new StringBuilder();
                //sb.Append("update Enblib set Name=@Name,");
                //sb.Append("Dept=@Dept,");
                //sb.Append("Sex=@Sex ");
                //sb.Append("where No=@No");
                //DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
                //db.AddInParameter(cmd, "@Name", DbType.String, textBox2.Text);
                //db.AddInParameter(cmd, "@Dept", DbType.String,  textBox3.Text);
                //db.AddInParameter(cmd, "@Sex", DbType.String, textBox4.Text);
                //db.AddInParameter(cmd, "@No", DbType.String, textBox1.Text);
                //int count = db.ExecuteNonQuery(cmd);
                //if (count > 0)
                //{
                //    MessageBox.Show("修改成功");
                //    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                //}
                //else
                //{
                //    MessageBox.Show("修改失败");
                //}
            }
            else
            {
            //    Database db = GetData.CreateDataBase();
            //    DataAccessor<Test_User> userAccessor;
            //    userAccessor = db.CreateSqlStringAccessor("insert into Enblib values(@no,@name,@dept,@sex)",
            //                       MapBuilder<Test_User>.MapAllProperties().
            //        Build()
            //);


                //Test_User addUser = new Test_User();
                //addUser.No = textBox1.Text;
                //addUser.Name = textBox2.Text;
                //addUser.Dept = textBox3.Text;
                //addUser.Sex = textBox3.Text;
                //Database db = GetData.CreateDataBase();
                //StringBuilder sb = new StringBuilder();
                //sb.Append("insert into Enblib values(@no,@name,@dept,@sex)");
                //DbCommand cmd = db.GetSqlStringCommand(sb.ToString());

                //db.AddInParameter(cmd, "@no", DbType.String, Guid.NewGuid().ToString());
                //db.AddInParameter(cmd, "@name", DbType.String, addUser.Name);
                //db.AddInParameter(cmd, "@dept", DbType.String, addUser.Dept);
                //db.AddInParameter(cmd, "@sex", DbType.String, addUser.Sex);
                //int count = db.ExecuteNonQuery(cmd);

                Database db = GetData.CreateDataBase();
                DataAccessor<Test_User> userAccessor;
                IRowMapper<Test_User> mapper = MapBuilder<Test_User>.BuildAllProperties();
                userAccessor = db.CreateSqlStringAccessor("insert into Enblib values('" +
                    Guid.NewGuid().ToString() + "','" +
                    textBox2.Text + "','" +
                    textBox3.Text + "','" +
                    textBox4.Text + "')", mapper);
                var list = userAccessor.Execute().ToList();
                if (list.Count == 0)
                {
                    MessageBox.Show("添加成功");
                    Home home = (Home)this.Owner;
                    home.showView();
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
}

        private void UserEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }
    }
}
